Completed
Push — master ( 589edc...c24ce2 )
by Yannick
35:38
created

map.common.js ➔ alliance   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 5
rs 9.4285
nop 1
1
/**
2
 * This javascript is part of FlightAirmap.
3
 *
4
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
5
 * Licensed under AGPL license.
6
 * For more information see: https://www.flightairmap.com/
7
*/
8
function getCookie(cname) {
9
    var name = cname + "=";
10
    var ca = document.cookie.split(';');
11
    for(var i=0; i<ca.length; i++) {
12
	var c = ca[i];
13
	while (c.charAt(0)==' ') c = c.substring(1);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
	if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
    }
16
    return "";
17
}
18
19
function delCookie(cname) {
20
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
21
}
22
23
function createCookie(name, value, days) {
24
    var date, expires;
25
    if (days) {
26
	date = new Date();
27
	date.setTime(date.getTime()+(days*24*60*60*1000));
28
	expires = "; expires="+date.toGMTString();
29
    } else {
30
	expires = "";
31
    }
32
    document.cookie = name+"="+value+expires+"; path=/";
33
}
34
35
function dynamicSort(property) {
36
    var sortOrder = 1;
37
    if(property[0] === "-") {
38
        sortOrder = -1;
39
        property = property.substr(1);
40
    }
41
    return function (a,b) {
42
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
43
        return result * sortOrder;
44
    }
45
}
46
47
function dynamicSortMultiple() {
48
    var props = arguments;
49
    return function (obj1, obj2) {
50
	var i = 0, result = 0, numberOfProperties = props.length;
51
	while(result === 0 && i < numberOfProperties) {
52
	    result = dynamicSort(props[i])(obj1, obj2);
53
	    i++;
54
	}
55
	return result;
56
    }
57
}
58
59
function mapType(selectObj) {
60
    var idx = selectObj.selectedIndex;
61
    var atype = selectObj.options[idx].value;
62
    var type = atype.split('-');
63
    if (type[0] == 'Mapbox') {
64
	createCookie('MapType',type[0],9999);
65
	createCookie('MapTypeId',type[1],9999);
66
	if (getCookie('Map2D3DSync')) {
67
	    createCookie('MapType3D',type[0],9999);
68
	    createCookie('MapType3DId',type[1],9999);
69
	}
70
    } else {
71
	createCookie('MapType',atype,9999);
72
	if (getCookie('Map2D3DSync')) {
73
	    createCookie('MapType3D',atype,9999);
74
	}
75
    }
76
    window.location.reload();
77
}
78
function mapType3D(selectObj) {
79
    var idx = selectObj.selectedIndex;
80
    var atype = selectObj.options[idx].value;
81
    var type = atype.split('-');
82
    if (type[0] == 'Mapbox') {
83
	createCookie('MapType3D',type[0],9999);
84
	createCookie('MapType3DId',type[1],9999);
85
	if (getCookie('Map2D3DSync')) {
86
	    createCookie('MapType',type[0],9999);
87
	    createCookie('MapTypeId',type[1],9999);
88
	}
89
    } else {
90
	createCookie('MapType3D',atype,9999);
91
	if (getCookie('Map2D3DSync')) {
92
	    createCookie('MapType',atype,9999);
93
	}
94
    }
95
    window.location.reload();
96
}
97
function clickSyncMap2D3D(cb) {
98
    createCookie('Map2D3DSync',cb.checked,9999);
99
    if (cb.checked) {
100
	createCookie('MapType3D',getCookie('MapType'),9999);
101
	createCookie('MapType3DId',getCookie('MapTypeId'),9999);
102
    }
103
}
104
105
function terrainType(selectObj) {
106
    var idx = selectObj.selectedIndex;
107
    var atype = selectObj.options[idx].value;
108
    var type = atype.split('-');
109
    document.cookie =  'MapTerrain='+type+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/'
110
    createCookie('MapTerrain',type,9999);
111
    if (type == 'stk') {
112
	stkterrain();
113
    } else if (type == 'articdem') {
114
	articterrain();
115
    } else if (type == 'ellipsoid') {
116
	ellipsoidterrain();
117
    } else if (type == 'vrterrain') {
118
	vrtheworldterrain();
119
    }
120
    //window.location.reload();
121
}
122
123
function sattypes(selectObj) {
124
    var sattypes = [], sattype;
125
    for (var i=0, len=selectObj.options.length; i< len;i++) {
126
	sattype = selectObj.options[i];
127
	if (sattype.selected) {
128
	    sattypes.push(sattype.value);
129
	}
130
    }
131
    createCookie('sattypes',sattypes.join(),2);
132
    updateSat();
133
}
134
function airlines(selectObj) {
135
    var airs = [], air;
136
    for (var i=0, len=selectObj.options.length; i< len;i++) {
137
	air = selectObj.options[i];
138
	if (air.selected) {
139
	    airs.push(air.value);
140
	}
141
    }
142
    createCookie('filter_Airlines',airs.join(),2);
143
}
144
function airlinestype(selectObj) {
145
    var idx = selectObj.selectedIndex;
146
    var airtype = selectObj.options[idx].value;
147
    createCookie('filter_airlinestype',airtype,2);
148
}
149
function racefilter(selectObj) {
150
    var idx = selectObj.selectedIndex;
151
    var race = selectObj.options[idx].value;
152
    if (race == 'all') {
153
	delCookie('filter_race');
154
    } else {
155
	createCookie('filter_race',race,2);
156
    }
157
    if (getCookie['MapFormat'] == '3d') {
158
	updateMarineData();
159
    } else {
160
	getLiveMarineData(0);
161
    }
162
}
163
function alliance(selectObj) {
164
    var idx = selectObj.selectedIndex;
165
    var alliance = selectObj.options[idx].value;
166
    createCookie('filter_alliance',alliance,2);
167
}
168
function identfilter() {
169
    var ident = $("#identfilter").value;
170
    createCookie('filter_ident',ident,2);
171
}
172
function mmsifilter() {
173
    var ident = $("#mmsifilter").value;
174
    createCookie('filter_mmsi',ident,2);
175
}
176
function removefilters() {
177
    // Get an array of all cookie names (the regex matches what we don't want)
178
    var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
179
    // Remove any that match the pattern
180
    for (var i = 0; i < cookieNames.length; i++) {
181
	if (/^filter_/.test(cookieNames[i])) {
182
	    delCookie(cookieNames[i]);
183
	}
184
    }
185
    window.location.reload();
186
}
187
function sources(selectObj) {
188
    var sources = [], source;
189
    for (var i=0, len=selectObj.options.length; i< len;i++) {
190
	source = selectObj.options[i];
191
	if (source.selected) {
192
	    sources.push(source.value);
193
	}
194
    }
195
    createCookie('filter_Sources',sources.join(),2);
196
}
197
198
199
function show2D() {
200
    createCookie('MapFormat','2d',10);
201
    if (document.getElementById("pointtype").className == 'tracker') {
202
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
203
    } else if (document.getElementById("pointtype").className == 'marine') {
204
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
205
    } else {
206
	createCookie('MapTrack',document.getElementById("pointident").className,1);
207
    }
208
    window.location.reload();
209
}
210
function show3D() {
211
    createCookie('MapFormat','3d',10);
212
    if (document.getElementById("pointtype").className == 'tracker') {
213
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
214
    } else if (document.getElementById("pointtype").className == 'marine') {
215
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
216
    } else {
217
	createCookie('MapTrack',document.getElementById("pointident").className,1);
218
    }
219
    window.location.reload();
220
}
221
function clickPolar(cb) {
222
    createCookie('polar',cb.checked,9999);
223
    window.location.reload();
224
}
225
function clickDisplayAirports(cb) {
226
    createCookie('displayairports',cb.checked,9999);
227
    window.location.reload();
228
}
229
function clickDisplayISS(cb) {
230
    createCookie('displayiss',cb.checked,9999);
231
    updateSat();
232
}
233
function clickDisplayMinimap(cb) {
234
    createCookie('displayminimap',cb.checked,9999);
235
    window.location.reload();
236
}
237
function clickShadows(cb) {
238
    createCookie('map3dnoshadows',cb.checked,9999);
239
    window.location.reload();
240
}
241
function clickSingleModel(cb) {
242
    createCookie('singlemodel',cb.checked,9999);
243
}
244
function clickUpdateRealtime(cb) {
245
    createCookie('updaterealtime',cb.checked,9999);
246
}
247
function clickVATSIM(cb) {
248
    createCookie('filter_ShowVATSIM',cb.checked,2);
249
}
250
function clickIVAO(cb) {
251
     createCookie('filter_ShowIVAO',cb.checked,2);
252
}
253
function clickphpVMS(cb) {
254
    createCookie('filter_ShowVMS',cb.checked,2);
255
}
256
function clickSBS1(cb) {
257
    createCookie('filter_ShowSBS1',cb.checked,2);
258
}
259
function clickAPRS(cb) {
260
    createCookie('filter_ShowAPRS',cb.checked,2);
261
}
262
function clickDisplayGroundStation(cb) {
263
    createCookie('show_GroundStation',cb.checked,2);
264
    window.location.reload();
265
}
266
function clickDisplayWeatherStation(cb) {
267
    createCookie('show_WeatherStation',cb.checked,2);
268
    window.location.reload();
269
}
270
/*
271
function clickDisplayWeather(cb) {
272
    createCookie('show_Weather',cb.checked,2);
273
//    window.location.reload();
274
}
275
*/
276
function clickDisplayLightning(cb) {
277
    createCookie('show_Lightning',cb.checked,2);
278
    window.location.reload();
279
}
280
function clickDisplayFires(cb) {
281
    createCookie('show_Fires',cb.checked,2);
282
    window.location.reload();
283
}
284
function clickDisplay2DBuildings(cb) {
285
    createCookie('Map2DBuildings',cb.checked,2);
286
    window.location.reload();
287
}
288
289
function unitdistance(selectObj) {
290
    var idx = selectObj.selectedIndex;
291
    var unit = selectObj.options[idx].value;
292
    createCookie('unitdistance',unit,9999);
293
}
294
function unitspeed(selectObj) {
295
    var idx = selectObj.selectedIndex;
296
    var unit = selectObj.options[idx].value;
297
    createCookie('unitspeed',unit,9999);
298
}
299
function unitcoordinate(selectObj) {
300
    var idx = selectObj.selectedIndex;
301
    var unit = selectObj.options[idx].value;
302
    createCookie('unitcoordinate',unit,9999);
303
}
304
function unitaltitude(selectObj) {
305
    var idx = selectObj.selectedIndex;
306
    var unit = selectObj.options[idx].value;
307
    createCookie('unitaltitude',unit,9999);
308
}
309
310
function addarchive(begindate,enddate) {
311
    console.log('Add archive');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
312
    createCookie('archive',true,2);
313
    createCookie('archive_begin',begindate,2);
314
    createCookie('archive_end',enddate,2);
315
    createCookie('archive_speed',document.getElementById("archivespeed").value,2);
316
    window.location.reload();
317
}
318
function noarchive() {
319
    console.log('Exit archive!');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
320
    delCookie('archive');
321
    delCookie('archive_begin');
322
    delCookie('archive_end');
323
    delCookie('archive_speed');
324
    window.location.reload();
325
}
326
function msgbox(text,buttontext) {
327
	buttontext = buttontext || "OK";
328
	$("<div>" + text + "</div>").dialog({
329
	    dialogClass: "no-close",
330
	    buttons: [{
331
		text: buttontext,
332
		click: function() {
333
		    $( this ).dialog( "close" );
334
		    $(this).remove();
335
		}
336
	    }]
337
	});
338
}
339
function generateRandomPoint (latitude,longitude,height,diff,radius) {
340
341
	//console.log('height: '+height+' - diff: '+diff);
342
	radius = Math.random()*radius;
343
	latitude = latitude*(Math.PI/180.0);
344
	longitude = longitude*(Math.PI/180.0);
345
	
346
	const sinLat = 	Math.sin(latitude)
347
	const cosLat = 	Math.cos(latitude)
348
349
	/* go fixed distance in random direction*/
350
	const bearing = Math.random() * Math.PI*2
351
	const theta = radius/6371000
352
	const sinBearing = Math.sin(bearing)
353
	const cosBearing = Math.cos(bearing)
354
	const sinTheta = Math.sin(theta)
355
	const cosTheta = Math.cos(theta)
356
    
357
	latitude = Math.asin(sinLat*cosTheta+cosLat*sinTheta*cosBearing);
358
	longitude = longitude + Math.atan2( sinBearing*sinTheta*cosLat, cosTheta-sinLat*Math.sin(latitude ));
359
	/* normalize -PI -> +PI radians */
360
	longitude = ((longitude+(Math.PI*3))%(Math.PI*2))-Math.PI
361
	var h = height+(Math.random()*diff)
362
	//console.log('h: '+h);
363
	return {
364
	    latitude: latitude/(Math.PI/180.0),
365
	    longitude: longitude/(Math.PI/180.0),
366
	    height: h
367
	};
368
}
369
function getColor(colorStart,colorEnd,colorCount,step) {
370
	var alpha = (1.0/colorCount)*step;
371
	return {
372
	    r: colorStart[0]*alpha+(1-alpha)*colorEnd[0],
373
	    v: colorStart[1]*alpha+(1-alpha)*colorEnd[1],
374
	    b: colorStart[2]*alpha+(1-alpha)*colorEnd[2]
375
	};
376
}
377
function convertDMS(coord,latlong) {
378
	if (latlong == 'latitude') {
379
		var nsew = (coord >= 0) ? 'N' : 'S';
380
	} else if (latlong == 'longitude') {
381
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 379. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
382
	}
383
	var coord = Math.abs(coord);
384
	var deg = Math.floor(coord);
385
	var min = Math.floor((coord - deg) * 60);
386
	var sec = Math.round((coord - deg - min / 60) * 3600);
387
	var result = deg+"° "+min+"' "+sec+'" '+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 380 is false. Are you sure this can never be the case?
Loading history...
388
	return result;
389
}
390
function convertDM(coord,latlong) {
391
	if (latlong == 'latitude') {
392
		var nsew = (coord >= 0) ? 'N' : 'S';
393
	} else if (latlong == 'longitude') {
394
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 392. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
395
	}
396
	var coord = Math.abs(coord);
397
	var deg = Math.floor(coord);
398
	var min = Math.round((coord - deg) * 60 *1000)/1000;
399
	var result = deg+"° "+min+"' "+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 393 is false. Are you sure this can never be the case?
Loading history...
400
	return result;
401
}
402